﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsVoteIp
    {
        public void Add(KeRui.Model.CmsVoteIp model)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserName", SqlDbType.VarChar, 50), new SqlParameter("@CreateIP", SqlDbType.VarChar, 20) };
            parameters[0].Value = model.VoteID;
            parameters[1].Value = model.UserID;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.CreateIP;
            DbHelperSQL.RunProcedure("CmsVoteIp_ADD", parameters);
        }

        public int CheckVote(int VoteID, int Limint, string AddressIP, int UserID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4), new SqlParameter("@Limint", SqlDbType.Int, 4), new SqlParameter("@AddressIP", SqlDbType.VarChar, 20), new SqlParameter("@UserID", SqlDbType.Int, 4) };
            parameters[0].Value = VoteID;
            parameters[1].Value = Limint;
            parameters[2].Value = AddressIP;
            parameters[3].Value = UserID;
            return DbHelperSQL.RunProcedure("CmsVoteItem_Vote", parameters, out num);
        }

        public void Delete(int IpAddressID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@IpAddressID", SqlDbType.Int, 4) };
            parameters[0].Value = IpAddressID;
            DbHelperSQL.RunProcedure("CmsVoteIp_Delete", parameters, out num);
        }

        public bool Exists(int IpAddressID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@IpAddressID", SqlDbType.Int, 4) };
            parameters[0].Value = IpAddressID;
            return (DbHelperSQL.RunProcedure("CmsVoteIp_Exists", parameters, out num) == 1);
        }

        public KeRui.Model.CmsVoteIp GetModel(int VoteID, string ip)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@VoteID", SqlDbType.Int, 4), new SqlParameter("@ip", SqlDbType.VarChar, 20) };
            parameters[0].Value = VoteID;
            parameters[1].Value = ip;
            KeRui.Model.CmsVoteIp ip2 = new KeRui.Model.CmsVoteIp();
            DataSet set = DbHelperSQL.RunProcedure("CmsVoteIp_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    ip2.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                if (set.Tables[0].Rows[0]["VoteID"].ToString() != "")
                {
                    ip2.VoteID = int.Parse(set.Tables[0].Rows[0]["VoteID"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    ip2.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
                }
                ip2.UserName = set.Tables[0].Rows[0]["UserName"].ToString();
                ip2.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    ip2.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return ip2;
            }
            return null;
        }

        public void Update(KeRui.Model.CmsVoteIp model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@VoteID", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@UserName", SqlDbType.VarChar, 50), new SqlParameter("@IpAddress", SqlDbType.VarChar, 20), new SqlParameter("@AddDate", SqlDbType.DateTime) };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.VoteID;
            parameters[2].Value = model.UserID;
            parameters[3].Value = model.UserName;
            parameters[4].Value = model.CreateIP;
            parameters[5].Value = model.CreateTime;
            DbHelperSQL.RunProcedure("CmsVoteIp_Update", parameters, out num);
        }
    }
}

